/** @format */

import { getUserRole } from "@/utils/auth";
import dynamicRouter from "@/router/dynamicRouter";
import router from "@/router";

const app = {
  state: {
    permission_status: false,
    user: null,
    activeRouter: "",
    permisionRouter: []
  },
  mutations: {
    SET_PERMISSION_STATUS(state, status) {
      state.permission_status = status;
    },
    SET_USER(state, data) {
      state.user = data;
    }
  },
  actions: {
    async FETCH_PERMISSION({ commit }) {
      // todo 获取动态路由
      const role = await getUserRole();
      const currentRouter = dynamicRouter[role];
      router.addRoutes(currentRouter);
      commit("SET_PERMISSION_STATUS", true);
      return currentRouter;
    }
  }
};

export default app;
